/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.gnucash.client.mvc;

import app.gnucash.client.AppEvents;
import app.gnucash.client.widget.LoginDialog;
import app.gnucash.client.widget.NavButtons;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.ui.RootPanel;

/**
 *
 * @author cc
 */
public class AppView extends View {

    public static final String WEST_PANEL = "west";
    public static final String VIEWPORT = "viewport";
    public static final String CENTER_PANEL = "center";
    private Viewport viewport;
    private ContentPanel west;
    private LayoutContainer center;
    private LoginDialog dialog;
    private String userName = " ";

    public AppView(Controller controller) {
        super(controller);
    }

    @Override
    protected void initialize() {
    }

    private void loginUI() {
        dialog = new LoginDialog();
        dialog.setClosable(false);
        dialog.show();
    }

    private void initUI() {
        viewport = new Viewport();
        viewport.setLayout(new BorderLayout());

        createNorth();
        createWest();
        createCenter();

        // registry serves as a global context
        Registry.register(VIEWPORT, viewport);
        Registry.register(WEST_PANEL, west);
        Registry.register(CENTER_PANEL, center);

        RootPanel.get().add(viewport);
    }

    private void createNorth() {
        StringBuilder sb = new StringBuilder();
        sb.append("<div id='demo-header' class='x-small-editor'><div id='demo-theme'></div><div id=demo-title>Welcome to GnuCash System</div></div>");

        HtmlContainer northPanel = new HtmlContainer(sb.toString());
        northPanel.setStateful(false);

        BorderLayoutData data = new BorderLayoutData(LayoutRegion.NORTH, 33);
        data.setMargins(new Margins());
        viewport.add(northPanel, data);
    }

    private void createWest() {
        BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST, 200, 150, 350);
        data.setMargins(new Margins(5, 0, 5, 5));

        west = new ContentPanel();
        west.setBodyBorder(false);
        west.setLayout(new FitLayout());  
        //west.setLayout(new AccordionLayout());
        //west.setLayoutOnChange(true);
        if (!userName.equals(" ")) {
            west.setHeading("欢迎你，" + userName);
        } else {
            west.setHeading("欢迎使用");
        }
        NavButtons navbt = new NavButtons();
        west.add(navbt);
        viewport.add(west, data);
    }

    private void createCenter() {
        center = new LayoutContainer();
        center.setLayout(new FitLayout());

        BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
        data.setMargins(new Margins(5, 5, 5, 5));

        viewport.add(center, data);
    }

    protected void handleEvent(AppEvent event) {
        if (event.getType() == AppEvents.Init) {
            userName = event.getData("user");
            initUI();
        } else if (event.getType() == AppEvents.Login) {
            loginUI();
        }
    }
}
